package com.ruoyi.business.controller;

import com.ruoyi.business.service.ITResourceProgressService;
import com.ruoyi.business.vo.TResourceProgressVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 学习进度Controller
 *
 * @author Tellsea
 * @date 2024-07-22
 */
@Api(tags = "学习进度Controller")
@RestController
@RequestMapping("/business/resourceProgress")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TResourceProgressController extends BaseController {

    private final ITResourceProgressService tResourceProgressService;

    @ApiOperation("查询学习进度列表")
    @PreAuthorize("@ss.hasPermi('business:resourceProgress:list')")
    @GetMapping("/list")
    public TableDataInfo<TResourceProgressVo> list(TResourceProgressVo entity) {
        return tResourceProgressService.queryList(entity);
    }

    @ApiOperation("查询学习进度所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TResourceProgressVo entity) {
        return AjaxResult.success("查询成功", tResourceProgressService.queryAll(entity));
    }

    @ApiOperation("导出学习进度列表")
    @PreAuthorize("@ss.hasPermi('business:resourceProgress:export')")
    @Log(title = "学习进度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TResourceProgressVo entity) {
        List<TResourceProgressVo> list = tResourceProgressService.queryAll(entity);
        ExcelUtil<TResourceProgressVo> util = new ExcelUtil<>(TResourceProgressVo.class);
        util.exportExcel(response, list, "学习进度数据");
    }

    @ApiOperation("获取学习进度详细信息")
    @PreAuthorize("@ss.hasPermi('business:resourceProgress:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tResourceProgressService.queryById(id));
    }

    @ApiOperation("新增学习进度")
    //@PreAuthorize("@ss.hasPermi('business:resourceProgress:add')")
    @Log(title = "学习进度", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TResourceProgressVo entity) {
        return tResourceProgressService.saveEntity(entity);
    }

    @ApiOperation("修改学习进度")
    @PreAuthorize("@ss.hasPermi('business:resourceProgress:edit')")
    @Log(title = "学习进度", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TResourceProgressVo entity) {
        return toAjax(tResourceProgressService.updateById(entity));
    }

    @ApiOperation("删除学习进度")
    @PreAuthorize("@ss.hasPermi('business:resourceProgress:remove')")
    @Log(title = "学习进度", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tResourceProgressService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("查询学习完成度")
    @GetMapping(value = "/getProgress")
    public AjaxResult getProgress() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return AjaxResult.success("查询成功", tResourceProgressService.getProgress(userId));
    }
}
